#include "precompiled.h"
#include "sslserver.h"
#include "common.h"
#include <protocol/message.h>

namespace Opl
{
	namespace Helpers
	{
		class SslServerPrivate
		{
			public:
				SslServerPrivate(): descriptor(-1)
				{}
				~SslServerPrivate() {}
				int descriptor;
		};


		SslServer::SslServer(int descriptor):
			Ssl::SslBase(0), O_PRIVATE_CREATE(SslServer)
		{
			O_D(SslServer);
			d->descriptor = descriptor;
		}

		SslServer::~SslServer()
		{
			O_PRIVATE_DELETE;
		}

		void SslServer::start()
		{
			O_D(SslServer);
			Q_ASSERT(d->descriptor >= 0);
			logger()->info("SslServer::start(descriptor=%1)", d->descriptor);
			QSslSocket* soc = new QSslSocket;
			soc->setSocketDescriptor(d->descriptor);
			setSocket(soc);
			Ssl::SslBase::start();
			socket()->setProtocol(QSsl::AnyProtocol);
			socket()->setPrivateKey("newkey.pem");
			socket()->setLocalCertificate("newcert.pem");
			if (!socket()->addCaCertificates("cacert.pem"))
			{
				logger()->error("Couldn't add CA certificates");
				return;
			}
		
			socket()->startServerEncryption();
			setStatusInternal(Connected);
		}
	}
}
